package org.bdware.irs.backend.dto;

import com.google.gson.JsonObject;
import org.bdware.irp.stateinfo.StateInfoBase;
import org.bdware.irs.backend.utils.DOIParser;

//前缀信息，存储前缀对应的机构信息
public class Organization {

    private Long id;
    //owner的doi号
    String doi;
    String name;
    String jwkKey;
    String protocol;
    String serviceAddress;
    String status;
    //分配的前缀
    String prefix;

    public Organization() {
    }

    //将信息转为标桩格式的状态信息
    public static Organization fromStateInfoBase(StateInfoBase stateInfoBase) {
        Organization organization = new Organization();
        organization.setDoi(stateInfoBase.getIdentifier());
        if (stateInfoBase.getHandleValues().get("name") != null)
            organization.setName(stateInfoBase.getHandleValues().get("name").getAsString());

        if (stateInfoBase.getHandleValues().get("jwkKey") != null)
            organization.setJwkKey(stateInfoBase.getHandleValues().get("jwkKey").getAsString());

        if (stateInfoBase.getHandleValues().get("protocol") != null)
            organization.setProtocol(stateInfoBase.getHandleValues().get("protocol").getAsString());

        if (stateInfoBase.getHandleValues().get("serviceAddress") != null)
            organization.setServiceAddress(stateInfoBase.getHandleValues().get("serviceAddress").getAsString());

        if (stateInfoBase.getHandleValues().get("status") != null)
            organization.setStatus(stateInfoBase.getHandleValues().get("status").getAsString());

        if (stateInfoBase.getHandleValues().get("prefix") != null)
            organization.setPrefix(stateInfoBase.getHandleValues().get("prefix").getAsString());
        return organization;
    }


    //将信息转为标桩格式的状态信息
    public StateInfoBase toStateInfoBase() {
        StateInfoBase stateInfoBase = new StateInfoBase();
        stateInfoBase.setIdentifier(prefix);
        JsonObject handleValues = new JsonObject();
        handleValues.addProperty("owner", doi);
        handleValues.addProperty("listeners", serviceAddress);
        handleValues.addProperty("pubKey", jwkKey);
        stateInfoBase.setHandleValues(handleValues);
        return stateInfoBase;
    }


    //将信息转为标桩格式的状态信息
    public StateInfoBase toStateInfoBase1() {
        StateInfoBase stateInfoBase = new StateInfoBase();
        stateInfoBase.setIdentifier(DOIParser.getAdminDOI(prefix));
        JsonObject handleValues = new JsonObject();
        handleValues.addProperty("doi", doi);
        handleValues.addProperty("name", name);
        handleValues.addProperty("jwkKey", jwkKey);
        handleValues.addProperty("protocol", protocol);
        handleValues.addProperty("serviceAddress", serviceAddress);
        handleValues.addProperty("status", status);
        handleValues.addProperty("prefix", prefix);
        stateInfoBase.setHandleValues(handleValues);
        return stateInfoBase;
    }

    public Organization(Long id, String doi, String name, String jwkKey, String protocol, String serviceAddress) {
        this.id = id;
        this.doi = doi;
        this.name = name;
        this.jwkKey = jwkKey;
        this.protocol = protocol;
        this.serviceAddress = serviceAddress;
    }

    public Organization(Long id, String doi, String name, String jwkKey, String protocol, String serviceAddress, String status) {
        this.id = id;
        this.doi = doi;
        this.name = name;
        this.jwkKey = jwkKey;
        this.protocol = protocol;
        this.serviceAddress = serviceAddress;
        this.status = status;
    }

    public Organization(Long id, String doi, String name, String jwkKey, String protocol, String serviceAddress, String status, String prefix) {
        this.id = id;
        this.doi = doi;
        this.name = name;
        this.jwkKey = jwkKey;
        this.protocol = protocol;
        this.serviceAddress = serviceAddress;
        this.status = status;
        this.prefix = prefix;
    }

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getDoi() {
        return doi;
    }

    public void setDoi(String doi) {
        this.doi = doi;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getJwkKey() {
        return jwkKey;
    }

    public void setJwkKey(String jwkKey) {
        this.jwkKey = jwkKey;
    }

    public String getProtocol() {
        return protocol;
    }

    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }

    public String getServiceAddress() {
        return serviceAddress;
    }

    public void setServiceAddress(String serviceAddress) {
        this.serviceAddress = serviceAddress;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}
